﻿using Common.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; 

namespace CMS6Tools.Management
{
    public class TextBoxOutLogger : global::Common.Logging.ILog
    {
        public TextBoxOutLogger(string logName, LogLevel logLevel, TextBox textBox)
        {
            LogName = logName;
            LogLevel = logLevel;
            writer = new TextBoxWriter(textBox);
        }

        private TextWriter writer;
        public string LogName { get; private set; }
        public LogLevel LogLevel { get; set; }

        protected virtual bool IsLogLevelEnabled(LogLevel logLevel)
        {
            return LogLevel != global::Common.Logging.LogLevel.Off
                && (int)logLevel >= (int)LogLevel;
        }

        protected virtual void OutputLog(LogLevel logLevel, object message, Exception exception = null)
        {
            if (IsLogLevelEnabled(logLevel))
            {
                writer.WriteLine(string.Format("{0},{1}.", message, exception != null ? string.Empty : exception.Message));
            }
        }

        public void Debug(object message, Exception exception)
        {
            OutputLog(LogLevel.Debug, message, exception);
        }

        public void Debug(object message)
        {
            OutputLog(LogLevel.Debug, message);
        }

        public void Error(object message, Exception exception)
        {
            OutputLog(LogLevel.Error, message, exception);
        }

        public void Error(object message)
        {
            OutputLog(LogLevel.Error, message);
        }

        public void Fatal(object message, Exception exception)
        {
            OutputLog(LogLevel.Fatal, message, exception);
        }

        public void Fatal(object message)
        {
            OutputLog(LogLevel.Fatal, message);
        }

        public void Info(object message, Exception exception)
        {
            OutputLog(LogLevel.Info, message, exception);
        }

        public void Info(object message)
        {
            OutputLog(LogLevel.Info, message);
        }

        public bool IsDebugEnabled
        {
            get { return IsLogLevelEnabled(LogLevel.Debug); }
        }

        public bool IsErrorEnabled
        {
            get { return IsLogLevelEnabled(LogLevel.Error); }
        }

        public bool IsFatalEnabled
        {
            get { return IsLogLevelEnabled(LogLevel.Fatal); }
        }

        public bool IsInfoEnabled
        {
            get { return IsLogLevelEnabled(LogLevel.Info); }
        }

        public bool IsTraceEnabled
        {
            get { return IsLogLevelEnabled(LogLevel.Trace); }
        }

        public bool IsWarnEnabled
        {
            get { return IsLogLevelEnabled(LogLevel.Warn); }
        }

        public void Trace(object message, Exception exception)
        {
            OutputLog(LogLevel.Trace, message, exception);
        }

        public void Trace(object message)
        {
            OutputLog(LogLevel.Trace, message);
        }

        public void Warn(object message, Exception exception)
        {
            OutputLog(LogLevel.Warn, message, exception);
        }

        public void Warn(object message)
        {
            OutputLog(LogLevel.Warn, message);
        }

    }
}
